package com.qf.md.unit.controller;

import com.qf.common.PageVO;
import com.qf.common.R;

import com.qf.entity.md.unit.MdUnitMeasure;
import com.qf.md.unit.param.DelBatchParam;
import com.qf.md.unit.param.UnitSearchParam;
import com.qf.md.unit.service.MdUnitMeasureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zdl
 * @date 2025/2/27
 */
@RestController
@RequestMapping("/md/unit")
@Api(tags = "计量单位模块")
public class MdUnitMeasureController {
    @Resource
    private MdUnitMeasureService mdUnitMeasureService;

    @ApiOperation(value = "计量单位上传")
    @PostMapping("/upload/excel")
    public R<?> upload(MultipartFile file){
        Long l = mdUnitMeasureService.uploadExcel(file);
        return R.ok(l);
    }

    @ApiOperation(value = "计量单位下载")
    @GetMapping("/download/excel")
    public R<?> download(HttpServletResponse response){
        mdUnitMeasureService.downExcel(response);
        return R.ok(null);
    }
    @ApiOperation("下载Excel模板")
    @GetMapping("/download/excel/template")
    public R<String> myExcelDownload(HttpServletResponse response) {
        mdUnitMeasureService.myExcelDownload(response);
        return R.ok("下载成功");
    }

    /**
     * 分页查询 + 模糊查询
     * @param unitSearchParam
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页模糊查询计量单位数据")
    public R<PageVO<MdUnitMeasure>> queryPage(UnitSearchParam unitSearchParam){
        PageVO<MdUnitMeasure> mdUnitMeasurePageVO = mdUnitMeasureService.queryUnitPage(unitSearchParam);
        return R.ok(mdUnitMeasurePageVO);
    }

    @GetMapping("/del/{measureId}")
    public R<Long> delByMeasureId(@PathVariable("measureId") Long measureId){
        return R.ok(mdUnitMeasureService.delUnitByMeasureId(measureId));
    }

    @PostMapping("/delBatch")
    public R<Long> delBatch(@RequestBody DelBatchParam delBatchParam){

        return R.ok(mdUnitMeasureService.delBatchMeasureByIds(delBatchParam.getIds()));
    }

    @GetMapping("/selectUnitByMeasureId")
    public R<MdUnitMeasure> selectUnitByMeasureId(Long measureId){
        return R.ok(mdUnitMeasureService.findByPrimaryKey(measureId));
    }

    @PostMapping("/add")
    public R<Long> addMeasure(@RequestBody MdUnitMeasure mdUnitMeasure){
        return R.ok(mdUnitMeasureService.addMeasure(mdUnitMeasure));
    }
    @GetMapping("/findPrimaryUnit")
    public R<List<MdUnitMeasure>> queryAllPrimaryFlag(){
        return R.ok(mdUnitMeasureService.queryAllPrimaryFlag());
    }

    @PostMapping("/update")
    public R<Long> updateMeasure(@RequestBody MdUnitMeasure mdUnitMeasure){
        System.out.println("=====>"+mdUnitMeasure);
        return R.ok(mdUnitMeasureService.updateMeasure(mdUnitMeasure));
    }
}
